![](https://danatec.vn/wp-content/uploads/2024/06/5-12-1024x503.webp)
Vue.JS check if Array,Object or String is empty: In Vue.js, To check if an array is empty in Vue.js, you can use the Array.length property, which returns the number of elements in the array To check if the string is empty in Vue.js, you can use the string.length property. This property returns the number of characters in the string, so if the string is empty, it will return 0. Here’s an example of You can use an if statement to check if the string.length property is equal to 0.To demonstrate this, we will create a simple example that uses the length property in VueJS
How to check if item exists in array in vue.js?
Checking if an array in Vue.js contains items can be done by using the “array.length” property.
Vue Js Check Array contains item
<div id="app">
<p v-if="demoArray.length === 0 ">Array is empty</p>
<p v-else v-for="itemArray in demoArray">{{itemArray}}</p>
</div>
<script type="module">
import { createApp } from "vue";
createApp({
data() {
return {
demoArray:[],
}
},
}).mount("#app");
</script>
Output of above example
![](https://danatec.vn/wp-content/uploads/2024/06/6-12.webp)
How to check if an array includes an object in Vue Js?
By using the Array.prototype.length property, you can quickly check whether an array of objects is empty in Vue.js without having to loop through each object manually You can use this example; we will show you how to use this property to determine whether an array of objects contains the following property:
Vue Js check if value exists in array of objects
<div id="app">
<p v-if="demoObject.length === 0 ">Array is empty</p>
<p v-else v-for="obj in demoObject">{{obj}}</p>
</div>
<script type="module">
import { createApp } from "vue";
createApp({
data() {
return {
demoObject:[
{fruit:'Apple',price:'134'},
{fruit:'Orange',price:'131'},
{fruit:'Banana',price:'124'}
],
}
},
}).mount("#app");
</script>
Output of above example
![](https://danatec.vn/wp-content/uploads/2024/06/7-12.webp)
In Vue.js, how do you check if a string is empty?
In Vue.js, you can check if a string is empty by using the JavaScript method ‘str.length’, which returns the number of characters in a string.This property returns the number of characters in the string, so if the string is empty, it will return 0. Here’s an example of how you can use this property to check if a string is empty To check if a string is empty, simply use the length property of the string to check if it is equal to 0
Vue Js check String is empty Example
<div id="app">
<p v-if="!str.length ">String is empty</p>
<p v-else>{{str}}</p>
</div>
<script type="module">
import { createApp } from "vue";
createApp({
data() {
return {
str:''
}
},
}).mount("#app");
</script>
Output of above example
![](https://danatec.vn/wp-content/uploads/2024/06/8-12.webp)